package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianOrderClient;
import com.diandian.backend.entrance.client.dto.OrderOnlineMeituanShangouSourceAddParamDTO;
import com.diandian.backend.entrance.client.dto.douyinlife.OrderOnlineDouyinLifeAddOneParamDTO;
import com.diandian.backend.entrance.client.dto.douyinlife.OrderOnlineDouyinLifeAddParam;
import com.diandian.backend.entrance.client.dto.douyinlife.OrderOnlineDouyinLifeUpdateParamParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterOrderService {

    private static final Logger logger = LoggerFactory.getLogger(RouterOrderService.class);

    @Autowired
    private DianDianOrderClient dianDianOrderClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data)  throws Exception {

        // 美团闪购订单新增
        if(EntranceConstant.ROUTER_ORDER_MEITUAN_SHANGOU_SOURCE_ADD.equals(cmd)){

            logger.info("执行美团闪购订单同步请求，参数: {}", data.toJSONString());

            // 获取配置信息
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            OrderOnlineMeituanShangouSourceAddParamDTO paramDTO = JSONObject.parseObject(data.toString(), OrderOnlineMeituanShangouSourceAddParamDTO.class);
            paramDTO.setBelong(belong);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = dianDianOrderClient.add(paramDTO);
            } catch (Exception e){
                logger.error("调用美团闪购订单同步服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("美团闪购订单同步结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 抖音本地生活-新增单个订单
        if(EntranceConstant.ROUTER_ORDER_DOU_YIN_LIFE_SINGLE_ADD.equals(cmd)){

            logger.info("执行抖音本地生活订单同步请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            OrderOnlineDouyinLifeAddOneParamDTO paramDTO = JSONObject.parseObject(data.toString(), OrderOnlineDouyinLifeAddOneParamDTO.class);
            paramDTO.setBelong(belong);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = dianDianOrderClient.syncDouYinLifeOne(paramDTO);
            } catch (Exception e){
                logger.error("调用抖音本地生活同步服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("抖音本地生活订单同步结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 抖音本地生活-修改订单状态
        if(EntranceConstant.ROUTER_ORDER_DOU_YIN_LIFE_UPDATE_STATUS.equals(cmd)){

            logger.info("执行抖音本地生活订单修改订单状态请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            OrderOnlineDouyinLifeUpdateParamParamDTO paramDTO = JSONObject.parseObject(data.toString(), OrderOnlineDouyinLifeUpdateParamParamDTO.class);
            paramDTO.setBelong(belong);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = dianDianOrderClient.updateDouYinLifeOneStatus(paramDTO);
            } catch (Exception e){
                logger.error("调用抖音本地生活修改订单状态服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("抖音本地生活订单修改订单状态结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 抖音本地生活-批量同步订单
        if(EntranceConstant.ROUTER_ORDER_DOU_YIN_LIFE_BATCH_ADD.equals(cmd)){

            logger.info("执行抖音本地生活批量同步订单状态请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            OrderOnlineDouyinLifeAddParam paramDTO = JSONObject.parseObject(data.toString(), OrderOnlineDouyinLifeAddParam.class);
            paramDTO.setBelong(belong);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = dianDianOrderClient.syncDouYinLifeBatch(paramDTO);
            } catch (Exception e){
                logger.error("调用抖音本地生活批量同步订单服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("抖音本地生活批量同步订单结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }


        throw new DefaultException("暂不支持此操作");
    }
}
